package org.lanqiao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.lanqiao.entity.Employee;
import org.lanqiao.service.EmployeeService;
import org.lanqiao.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

//    @Autowired
//    private EmployeeService employeeService;
//
//
//    /**
//     * 员工登录功能
//     * @param request
//     * @param employee
//     * @return
//     */
//    @PostMapping("/login")
//    public Result<Employee> Login(HttpServletRequest request ,  @RequestBody Employee employee){
//
//        //将页面获取的密码进行MD5加密
//        String password = employee.getPassword();
//        password = DigestUtils.md5DigestAsHex(password.getBytes());
//
//        //根据页面提交的用户名查询数据库
//        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("username" , employee.getUsername());
//        Employee one = employeeService.getOne(queryWrapper);
//
//        //判断用户是否存在
//        if(one == null){
//            return Result.error("该用户不存在");
//        }
//
//        //判断密码是否正确
//        if(!one.getPassword().equals(password)){
//            return Result.error( "密码错误");
//        }
//
//        //判断该用户账号状态是否可用
//        if(one.getStatus() == 0){
//            return Result.error("账号已禁用");
//        }
//        //登陆成功，将员工id存入Session中
//        request.getSession().setAttribute("employee" , one.getId());
//        return Result.success(one);
//    }
//
//    /**
//     * 员工退出
//     * @param request
//     * @return
//     */
//    @PostMapping("/logout")
//    public Result<String> Logout(HttpServletRequest request){
//        request.getSession().removeAttribute("employee");
//        return Result.success("退出成功");
//    }
//
//    /**
//     * 添加员工
//     * @param request
//     * @param employee
//     * @return
//     */
//    @PostMapping
//    public Result<String> addEmployee(HttpServletRequest request , @RequestBody Employee employee){
//
//        //设置员工初始密码123456，使用MD5加密
//        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//
//        /*//设置员工账号创建时间
//        employee.setCreateTime(LocalDateTime.now());
//
//        //设置更新时间
//        employee.setUpdateTime(LocalDateTime.now());
//
//        //获取当前登录账号id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);*/
//
//        employeeService.save(employee);
//        return Result.success("新增员工成功");
//    }
//
//    /**
//     * 分页查询
//     * @param page
//     * @param pageSize
//     * @param name
//     * @return
//     */
//    @GetMapping("/page")
//    public Result<Page> page(int page , int pageSize , String name){
//
//        //构造分页构造器
//        Page pageInfo = new Page(page , pageSize);
//
//        //构造条件构造器
//        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
//
//        //添加过滤条件
//        queryWrapper.like(!StringUtils.isEmpty(name) , Employee::getName , name);
//
//        //添加排序条件
//        queryWrapper.orderByDesc(Employee::getUpdateTime);
//
//        //执行查询
//        employeeService.page(pageInfo , queryWrapper);
//        return Result.success(pageInfo);
//    }
//
//
//    /**
//     * 修改员工信息
//     * @param employee
//     * @return
//     */
//    @PutMapping
//    public Result<String> update(HttpServletRequest request , @RequestBody Employee employee){
//        /*Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(empId);
//        employee.setUpdateTime(LocalDateTime.now());*/
//        employeeService.updateById(employee);
//        return Result.success("修改成功");
//    }
//
//    /**
//     * 根据id查询员工信息
//     * @param id
//     * @return
//     */
//    @GetMapping("{id}")
//    public Result<Employee> getEmployeeById(@PathVariable Long id){
//        Employee employee = employeeService.getById(id);
//        return Result.success(employee);
//    }
    @Autowired
    EmployeeService employeeService;


    /**
     * 登录
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public Result<Employee> login(@RequestBody Employee employee , HttpServletRequest request){

        String password = employee.getPassword();
         password = DigestUtils.md5DigestAsHex(password.getBytes());

        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username" , employee.getUsername());
        //queryWrapper.eq("password" , employee.getPassword());

        Employee one = employeeService.getOne(queryWrapper);

        if(one == null){
            return Result.error("该用户不存在");
        }

        if(!password.equals(one.getPassword())){
            return Result.error("密码错误");
        }

        if("0".equals(one.getStatus())){
            return Result.error("该账号已冻结");
        }

        //将用户的id放到缓存中
        request.getSession().setAttribute("employee" , one.getId());
        return Result.success(one);

    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(int page , int pageSize , String name){
        Page pageInfo = new Page(page , pageSize);

        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(name) , "name" , name);

        employeeService.page(pageInfo , queryWrapper);
        return Result.success(pageInfo);

    }

}
